use glium::{glutin::{ContextBuilder, window::WindowBuilder, event_loop::EventLoop}, Display, Surface};

use crate::{IProjector, Space, Camera};

use super::{GLProjector, GLSpace};

impl GLProjector {
    pub fn new(wb: WindowBuilder, event_loop: &EventLoop<()>) -> Self {
        let ctx = ContextBuilder::new()
            .with_depth_buffer(24)
            .build_windowed(wb, &event_loop)
            .unwrap();
        let display = Display::from_gl_window(ctx).unwrap();
        Self {
            display,
            scene: None,
        }
    }

    pub fn display(&self) -> &Display {
        &self.display
    }
}

impl IProjector for GLProjector {
    fn set_scene(&mut self, space: &Space) {
        let scene = GLSpace {
            bg_color: space.background_color(),
        };

        self.scene = Some(scene);
    }

    fn play_film(&mut self, camera: &Camera) {
        let mut frame = self.display().draw();

        if let Some(scene) = &self.scene {
            frame.clear_color_and_depth(scene.background_color().tuple(), 1.0);
            frame.finish().unwrap();
        }
    }
}